Skip to content

Method: annotationPropertyValuesToAxioms(NamedResource, Collection)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
18: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
19: import cz.cvut.kbss.ontodriver.model.Assertion;
20: import cz.cvut.kbss.ontodriver.model.Axiom;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import org.semanticweb.owlapi.model.*;
23:
24: import java.net.URI;
25: import java.util.ArrayList;
26: import java.util.Collection;
27: import java.util.Set;
28: import java.util.stream.Collectors;
29:
30: class ExplicitAxiomLoader implements AxiomLoader {
31:
32: private static final Assertion UNSPECIFIED_ASSERTION = Assertion.createUnspecifiedPropertyAssertion(false);
33:
34: private final OWLOntology ontology;
35: private final OWLDataFactory dataFactory;
36:
37: private final OwlapiAdapter adapter;
38: private final AxiomAdapter axiomAdapter;
39:
40: private Set<Assertion> assertions;
41: private Set<URI> assertionUris;
42:
43: ExplicitAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
44: this.adapter = adapter;
45: this.ontology = snapshot.getOntology();
46: this.dataFactory = snapshot.getDataFactory();
47: this.axiomAdapter = new AxiomAdapter(dataFactory, adapter.getLanguage());
48: }
49:
50: @Override
51: public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
52: this.assertions = assertions;
53: this.assertionUris = assertions.stream().map(NamedResource::getIdentifier).collect(Collectors.toSet());
54: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
55: final Collection<Axiom<?>> axioms = new ArrayList<>();
56: if (assertions.contains(Assertion.createClassAssertion(false))) {
57: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, false));
58: }
59: // This involves a lot of filtering, perhaps we should use EntitySearcher and look for values of concrete properties
60: final Collection<OWLDataPropertyAssertionAxiom> dpAssertions = ontology.getDataPropertyAssertionAxioms(
61: individual);
62: axioms.addAll(dataPropertyValuesToAxioms(subject, dpAssertions));
63: final Collection<OWLObjectPropertyAssertionAxiom> opAssertions = ontology.getObjectPropertyAssertionAxioms(
64: individual);
65: axioms.addAll(objectPropertyValuesToAxioms(subject, opAssertions));
66: final Collection<OWLAnnotationAssertionAxiom> apAssertions = ontology.getAnnotationAssertionAxioms(
67: individual.getIRI());
68: axioms.addAll(annotationPropertyValuesToAxioms(subject, apAssertions));
69: return axioms;
70: }
71:
72: private Collection<Axiom<?>> dataPropertyValuesToAxioms(NamedResource subject,
73: Collection<OWLDataPropertyAssertionAxiom> axioms) {
74: return axioms.stream().filter(axiom ->
75: assertions.contains(UNSPECIFIED_ASSERTION) ||
76: assertionUris.contains(axiom.getProperty().asOWLDataProperty().getIRI().toURI()))
77: .map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
78: .collect(Collectors.toList());
79: }
80:
81: private Collection<Axiom<?>> objectPropertyValuesToAxioms(NamedResource subject,
82: Collection<OWLObjectPropertyAssertionAxiom> axioms) {
83: return axioms.stream().filter(axiom ->
84: assertions.contains(UNSPECIFIED_ASSERTION) ||
85: assertionUris.contains(axiom.getProperty().asOWLObjectProperty().getIRI().toURI()))
86: .map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
87: .collect(Collectors.toList());
88: }
89:
90: private Collection<Axiom<?>> annotationPropertyValuesToAxioms(NamedResource subject,
91: Collection<OWLAnnotationAssertionAxiom> axioms) {
92: return axioms.stream().filter(axiom ->
93: assertions.contains(UNSPECIFIED_ASSERTION) ||
94: assertionUris.contains(axiom.getProperty().asOWLAnnotationProperty().getIRI().toURI()))
95: .map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
96: .collect(Collectors.toList());
97: }
98:
99: @Override
100: public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
101: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
102: final Collection<Axiom<?>> axioms = new ArrayList<>();
103: ontology.getDataPropertyAssertionAxioms(individual)
104: .forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
105: ontology.getObjectPropertyAssertionAxioms(individual)
106: .forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
107: ontology.getAnnotationAssertionAxioms(individual.getIRI())
108: .forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
109: return axioms;
110: }
111: }